package com.nit.elasticsearch.moudles.jd.utils;

import com.nit.elasticsearch.moudles.jd.entity.Content;
import lombok.SneakyThrows;
import lombok.experimental.UtilityClass;
import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.jsoup.select.Elements;

import java.net.URL;
import java.util.ArrayList;
import java.util.List;

/**
 * @author :fjw
 * @description:html解析工具类
 * @date :2020/11/14 12:46
 */
@UtilityClass
public class HtmlParseUtil {
	public static final String BASE_URL = "https://search.jd.com/Search?keyword={keyword}&page={page}";
	public static void main(String[] args) {
		List<Content> java = HtmlParseUtil.parseJD("java",1);
		for (Content content : java) {
			System.out.println(content);
		}
	}

	//爬取数据
	@SneakyThrows
	public List<Content> parseJD(String keyword,Integer page) {
		ArrayList<Content> contents = new ArrayList<>();
		//解析网页
		String url = BASE_URL.replaceAll("\\{keyword}", keyword).replaceAll("\\{page}", String.valueOf(page));
		Document document = Jsoup.parse(new URL(url), 30000);
		//获取元素
		Element element = document.getElementById("J_goodsList");
		//获取所有li元素
		Elements lis = element.getElementsByTag("li");
		for (Element li : lis) {
			//关羽图片特别多的网站,所有图片都是延迟加载的
			String img = li.getElementsByTag("img").eq(0).attr("data-lazy-img");
			String price = li.getElementsByClass("p-price").eq(0).text();
			String title = li.getElementsByClass("p-name").eq(0).text();
			Content content = new Content().setImg(img).setPrice(price).setTitle(title);
			contents.add(content);
		}
		return contents;
	}

}
